package org.zhangsh;

import org.jodconverter.OfficeDocumentConverter;
import org.jodconverter.office.LocalOfficeManager;
import org.jodconverter.office.OfficeManager;

import java.io.File;

public class App {

    public static void main(String[] args) throws Exception {

        File docxPath = new File("/E:/123.docx");
        File targetPath = new File("/E:/1111.pdf");

        // 方式一,传统方式，优点不需指定openOffice或者LibreOffice安装目录，缺点，需要手动开启office服务
		/*OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
        try {
            connection.connect();
            DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
            converter.convert(new FileInputStream(docxPath),new DefaultDocumentFormatRegistry().getFormatByFileExtension("docx")
            		, new FileOutputStream(targetPath),new DefaultDocumentFormatRegistry().getFormatByFileExtension("pdf"));

        } catch (Exception e) {
        	throw new RuntimeException("office2pdf()转换器异常！", e);
        }finally{
        	connection.disconnect();
        }*/
        // 方式二，两种方法，优点自动开启服务，缺点，需要指定安装目录
        OfficeManager officeManager = LocalOfficeManager.builder()
                .officeHome("D:/Program Files/LibreOffice6.0.6/")
//                .install()    // 这句在使用方放二时加上
                .build();
        officeManager.start();
        // 方法一
        OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
        converter.convert(docxPath, targetPath);
        // 方方法二
//        JodConverter.convert(docxPath).to(targetPath).execute();

        officeManager.stop();
        System.out.println("end...");
    }
}
